package com.hjq.fcloud.utils;

public class StringSplitter {

    public static String splitAndJoinString(String str, int maxLength, String join) {
        if (str == null || str.length() <= maxLength) {
            // 如果字符串为空、为null或者长度不大于maxLength，则直接返回原字符串
            return str;
        }

        // 用于存储分割后的字符串
        StringBuilder[] parts = new StringBuilder[(int) Math.ceil((double) str.length() / maxLength)];

        int index = 0;
        for (int i = 0; i < str.length(); i += maxLength) {
            // 分割字符串，每次最多maxLength个字符
            String part = str.substring(i, Math.min(str.length(), i + maxLength));
            parts[index++] = new StringBuilder(part);
        }

        // 使用StringBuilder来构建最终的字符串，以便高效连接
        StringBuilder finalString = new StringBuilder();
        for (int i = 0; i < parts.length; i++) {
            if (i > 0) {
                // 不是第一个部分时，前面添加换行符
                finalString.append(join);
            }
            finalString.append(parts[i]);
        }

        return finalString.toString();
    }
}
